var express = require('express');
var router = express.Router();

const Query = require('../utils/db.js')
let { api_url } = require('../utils/config.js')

/* GET home page. */
router.get('/', async function(req, res, next) {
 res.send('课程详情')
});


router.get('/course', async function(req, res, next) {
//    let id='1beec178-4b02-40fb-9d1c-8aa61cd4a097'

let{id=''}=req.query

if(id==''){
    next({
        msg:'请输入必要参数',
        status:500
    })
    return
}



   let sql1=`SELECT *, CONCAT('${api_url}','/uploads/courses/',image_src) AS image_src FROM e_course WHERE cid='${id}'`
   let res1=await Query(sql1)
   let course=res1.result[0]

   if(res1.result.length==0){
    next({
        msg:'请传入一个正确的课程id',
        status:500
    })
    return
   }
   delete course.detail_imgs


   let sql2=`SELECT *,CONCAT('${api_url}','/uploads/courses/',head_photo_url) AS head_photo_url FROM e_teacher WHERE tid='${course.tid}'`
   
   let res2=await Query(sql2)
   let teacher=res2.result[0]


   let sql3=`SELECT id,cid,vnum,video_title FROM e_video WHERE cid='${id}'`
   let res3=await Query(sql3)
   let video=res3.result

   let data={
       msg:"ok",
       status:200,
       result:{
           course:course,
           teacher:teacher,
           video:video
       }
   }
     res.send(data)
   });



module.exports = router;